//Date: 2024-12-26 10:24:00
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int rd() {
    int s=0,m=0;char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-')m=1;ch=getchar();}
    while( isdigit(ch)) s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
    return m?-s:s;
}
bool MBE;
namespace SOLVER {
int n,q,c,c1,c2,fa[200005][18],S[200005][18],ans[200005],dfn[200005],in[200005],out[200005],tim;
vector<int> g[200005],add[200005],del[200005];vector<pair<int,int>> qry[200005];
void dfs(int u,int p) {
    for(int i=1;i<=17;i++) fa[u][i]=fa[fa[u][i-1]][i-1],S[u][i]=S[u][i-1]|S[fa[u][i-1]][i-1];
    dfn[in[u]=++tim]=tim;int now=u,st=S[u][0];add[in[u]].emplace_back(st);
    while(1) {
        for(int i=17;i>=0;i--) if((S[now][i]|st)==st) now=fa[now][i];
        del[in[now]].emplace_back(st);if(now==0) break;
        st|=S[now][0],add[in[now]].emplace_back(st),now=fa[now][0];
    }
    for(int v:g[u]) dfs(v,u);out[u]=tim;
}
int b[(1<<9)+5][(1<<6)+5],b1[(1<<15)+5];
int query(int st) {int st1=st>>c2,st2=st-(st1<<c2),sum=b[0][st2];for(int i=st1;i!=0;i=(i-1)&st1) sum+=b[i][st2];return sum;}
void update(int st,int v) {int st1=st>>c2,st2=st-(st1<<c2);for(int i=st2;i<(1<<c2);i=(i+1)|st2) b[st1][i]+=v;}
void MAIN() {
    cin>>n>>q>>c;c1=(int)(c*0.6+0.5),c2=c-c1;for(int i=1;i<=n;i++) S[i][0]=(1<<rd());
    for(int i=2;i<=n;i++) fa[i][0]=rd(),g[fa[i][0]].emplace_back(i);dfs(1,0);
    for(int i=1;i<=q;i++) {
        int st=rd(),u=rd();if((S[u][0]|st)!=st) {ans[i]=1;continue;}
        for(int j=17;j>=0;j--) if(fa[u][j]&&(S[u][j]|S[fa[u][j]][0]|st)==st) u=fa[u][j];
        qry[in[u]-1].emplace_back(st,-i);qry[out[u]].emplace_back(st,i);
    }
    for(int i=0;i<=n;i++) {
        for(int x:add[i]) update(x,1);for(int x:del[i]) update(x,-1);
        for(auto x:qry[i]) ans[abs(x.second)]+=x.second/abs(x.second)*query(x.first);
    }
    for(int i=1;i<=q;i++) printf("%lld\n",ans[i]);
}
}
bool MED;
signed main() {
    freopen("tree.in","r",stdin);freopen("tree.out","w",stdout);
    for(int tt=1;tt;tt--) SOLVER::MAIN();
    cerr<<(&MBE-&MED)/1024<<" KB, "<<1000*clock()/CLOCKS_PER_SEC<<" ms\n";
    return 0;
}